// pages/Players/checkentrance/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    playermatchinfo:'',
    error:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
      //检查是否登录
      console.log(app.globalData.userInfo);
    if (app.globalData.userInfo.openid && app.globalData.userInfo.session_key) {

      console.log(app.globalData.userInfo);
      this.setData({
        islogin: true
      })
    }

    if (!this.data.islogin && wx.getStorageSync('autoreg') == "1") {
      var that = this;
      app.wxlogin(app, function () {

        that.setData({
          islogin: true
        });
        that.onShow();
      });
      return;
    }

    if (!this.data.islogin) {
      this.login();
      return;
    }

    var userInfo = app.globalData.userInfo;
    if (userInfo != undefined && userInfo != null) {
      this.setData({
        userInfo: userInfo
      });
      // var obj = this.selectComponent("#register");
      // obj.filldata(userInfo.data);
    }
    if(!userInfo.userID || !userInfo.player_id){
      wx.showModal({
        title: '请先去绑定球员信息',
        confirmText: "确定",
        cancelText: "取消",
        success: function (res) {
          if (res.confirm) {
            console.log(res);
            wx.navigateTo({
              url: '../../more/information/information',
            })

            return;
          } else {
            wx.switchTab({
              url: '../../index/index',
            })
            return;
          }

        }
      })
    
      return ;
    }
    this.getmatchregisterinfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  
  login: function () {

    if (!app.checkislogin()) {
      wx.showModal({
        title: '请先登陆才能查看',
        confirmText: "确定",
        cancelText: "取消",
        success: function (res) {
          if (res.confirm) {
            console.log(res);
            wx.navigateTo({
              url: '../../login/login',
            })

            return;
          } else {
            wx.switchTab({
              url: '../../index/index',
            })
            return;
          }

        }
      })
    }


  },

  _errorshow: function (e) {
    var msg = e;
    console.log(msg);
    this.setData({
      error: ''
    });
    this.setData({
      error: msg
    })
  },

  gotocenter: function () {
    wx.navigateTo({
      url: '/pages/More/information/information',
    })
  },

  
  changedata: function (e) {
    var playermatchinfo = this.data.playermatchinfo;
    var value = e.detail.value;
    var id = e.target.id;
   

    console.log(e);
    if (id == "PlayerInDate") {
      playermatchinfo.PlayerInDate = value;
    }
    if (id == "PlayerSize") {
      playermatchinfo.PlayerSize = this.data.PlayerSize[value];
    }
    if (id == "PlayerBackDate") {
      playermatchinfo.PlayerBackDate = value;
    }
  
    
    

    this.setData({
      playermatchinfo: playermatchinfo
    })

    console.log(this.data.playermatchinfo);
  },

  getmatchregisterinfo(){
    var that = this;
    app.getmatchregisterinfo(this.data.playermatchinfo.reg_id,function(res){
        if(res.data.data.playermatchinfo){
          that.setData({playermatchinfo:res.data.data.playermatchinfo,
            PlayerSize:res.data.data.PlayerSize});
        }
    },function(res){

    })
  },

  inputdata: function (e) {
    var playermatchinfo = this.data.playermatchinfo;
    var cat = e.target.id;
    var value = e.detail.value;
    console.log("inputdata");
    console.log(e);
    if (cat == "Playerhdcp") {
      playermatchinfo.Playerhdcp = value;
    }
    if (cat == "PlayerInFlightNO") {
      playermatchinfo.PlayerInFlightNO = value;
    }
    if (cat == "PlayerBackFlightNO") {
      playermatchinfo.PlayerBackFlightNO = value;
    }
     if (cat == "PlayerBackFlightNO") {
      playermatchinfo.PlayerBackFlightNO = value;
    }
    if (cat == "PlayerRemark") {
      playermatchinfo.PlayerRemark = value;
    }
    console.log(playermatchinfo);
    this.setData({
      playermatchinfo: playermatchinfo
    });
  },
  
  commit: function () {
    var playermatchinfo = this.data.playermatchinfo;

   
    if(!playermatchinfo.Playername){
      this._errorshow("没有能有参与半决赛的球员");
      return;
    }

    if (!playermatchinfo.Playerhdcp) {
      this._errorshow("请输入差点");
      return;
    } else if (!playermatchinfo.PlayerSize) {
      this._errorshow("请选择衣服尺码");
      return;
    } 

    var that = this;
    this.setData({
      iscommitregister: true
    })
    wx.showLoading({
      title: "正在提交确认参赛申请"
    });
    app.commitmatchregister( playermatchinfo, function (res) {
      if (res.data.errorcode.code == "ok") {

        that.setData({
          iscommitregister: false
        })
        var reg_id = res.data.data;
        if (reg_id) {
          
          //that.getfiledlist(false);
          wx.requestSubscribeMessage({
            tmplIds: that.data.submsg,
            success (res) { },
            fail(res){console.log(res)}
          });
            //支付成功跳转
            //跳转到成功页面
            var succuss = {
              message: that.data.playermatchinfo.Playername + "\n" + that.data.playermatchinfo.PlayerMatchname + "\n 确认参赛成功",
              time: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
              back: true,
              buttoninfo: "返回"
            }
            // wx.hideLoading();

            wx.navigateTo({
              url: '/pages/succuss/index?succuss=' + JSON.stringify(succuss)
            })
          // that.wxpay(register_id);
        }
      } else {
        wx.showToast({
          title: res.data.errorcode.message,
          icon: 'error',
          duration: 5000
        })
      }
    }, function () {
      wx.hideLoading();
      that.setData({
        iscommitregister: false
      })
    });

  },
})